﻿using System.Collections.Generic;
using Engine;
using Game;
using GameEntitySystem;

namespace Mekiasm.Common
{
    public class Element<T> where T : class
    {
        public Point3 Point;
        public Vector3 Position;
        public bool Visited;
        public List<Element<T>> Connections = new List<Element<T>>();
        public SoundItem Sound;
        public int DeviceID;
        public Interface.IMekMachine Instance;
        public Element(Point3 p, bool AutoFind = true)
        {
            Init(p, AutoFind);
        }

        public Element(int x, int y, int z, bool AutoFind = true)
        {
            Point3 p = new Point3(x, y, z);
            Init(p, AutoFind);
        }
        public void Init(Point3 p, bool AutoFind)
        {
            Point = p;
            Position = new Vector3(p);
            if (AutoFind) FindComponent();
        }
        public void FindComponent()
        {
            ComponentBlockEntity componentBlockEntity = ILibrary.SubsystemBlockEntities.GetBlockEntity(Point.X, Point.Y, Point.Z);
            if (componentBlockEntity == null) return;
            foreach (Component component in componentBlockEntity.Entity.Components)
            {
                if (component is Interface.IMekMachine machine)
                {
                    Instance = machine;
                    DeviceID = ILibrary.GetBasicValue(ILibrary.SubsystemTerrain.Terrain.GetCellValueFast(Point.X, Point.Y, Point.Z));
                    return;
                }
            }
            if (Instance == null) throw new System.Exception("添加Elemnt[" + GetType().Name + "]失败，未能找到实体");
        }

        public virtual bool Simulate()
        {
            return true;
        }

        public virtual void PlayRunningParticle()
        {
        }

        public virtual void PlaySound()
        {
            if (Instance.Machine.IsCrafting)
            {
                //声音需要实时计算
                foreach (var player in ILibrary.Project.FindSubsystem<SubsystemPlayers>().ComponentPlayers)
                {
                    Vector3 vector3 = player.ComponentBody.Position;
                    float f = Vector3.Distance(Position, vector3);
                    if (f >= 16) f = 0;
                    else if (f <= 4f) f = 1f;
                    else f = (16 - f) / 12;
                    Sound.streamingSound.Volume = f * SettingsManager.SoundsVolume;
                    if (f == 0) Sound.streamingSound.Pause();
                    Sound.streamingSound.Play();
                }
            }
            else
            {
                Sound.streamingSound.Pause();
            }
        }

        public int Point3ToFace(Point3 p)
        {
            for (int i = 0; i < 6; i++)
            {
                if (CellFace.m_faceToPoint3[i] == p)
                {
                    return i;
                }
            }
            throw new System.Exception("无效的Point");
        }
        public static int OppositeFace(int face)
        {
            return CellFace.m_oppositeFaces[face];
        }
        //能量交换
        public long GetAvaiablePower(long Now, long InputMax, long Max, long From)
        {
            long remain = Max - Now;
            if (remain >= InputMax)
            {
                if (From >= InputMax) return InputMax;
                else return 0;
            }
            return 0;
        }
        public virtual void SetObject(object obj)
        {


        }
    }
}
